package com.dkd.flow.strategy.service.operation;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dkd.common.core.domain.entity.SysUser;
import com.dkd.flow.context.ProcessContext;
import com.dkd.flow.context.ProcessContextBuilder;
import com.dkd.flow.domain.EProcessInstances;
import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.emums.FlowOperation;
import com.dkd.flow.emums.FlowStatus;
import com.dkd.flow.emums.TaskActionType;
import com.dkd.flow.emums.TaskStatus;
import com.dkd.flow.service.impl.EProcessTaskServiceImpl;
import com.dkd.flow.strategy.annotation.Strategy;
import com.dkd.flow.utils.RevokeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Strategy(FlowOperation.AGREE)
public class AgreeStrategy extends BaseStrategy {
    @Autowired
    ProcessContextBuilder processContextBuilder;
    @Autowired
    private EProcessTaskServiceImpl eProcessTaskServiceImpl;
    @Autowired
    public RevokeUtils revokeUtils;
    @Override
    public Long execute(JSONObject data) {
        // 使用构建器初始化上下文 --- start --- //
        ProcessContext context = processContextBuilder.build(data);
        Long curTaskId = context.getCurTaskId();
        EProcessInstances eProcessInstances = context.getEProcessInstances();
        EProcessTask curTask = context.getCurTask();
        JSONObject formValue = context.getFormValue();
        Long processDefinitionId = context.getProcessDefinitionId();
        Long processInstancesId = context.getProcessInstancesId();
        // 使用构建器初始化上下文 --- end --- //
        initNodeRelations(processDefinitionId);
        JSONObject toNode = links.get(0).getJSONObject("toNode");
        if (toNode == null) {
            throw new RuntimeException("links 中第一个节点的 toNode 为 null，无法创建发起人任务");
        }
        SysUser liableUser = nodeUtils.getLiableUser(eProcessInstances);
        Long taskGroupId = taskCreate.getTaskGroupId(eProcessInstances);
        // 创建 审批人任务 APPROVER_TASK 2 自己 已审批 APPROVAL 3 处理中 PROCESSING 1
        Long approverTaskId = IdUtil.getSnowflakeNextId();
        EProcessTask approverTask = taskCreate.approverTask(eProcessInstances,
                                                            nodeUtils.getApproverByTask(curTask),
                                                            curTask.getProcessDesignId(),
                                                            TaskStatus.APPROVAL,
                                                            approverTaskId,
                                                            approverTaskId,
                                                            null,
                                                            taskGroupId,
                                                            JSONObject.toJSONString(formValue),
                                                            TaskActionType.OPERATION_TASK);
        // 创建 发起人任务 INITIATOR_TASK 1 上一个 已审批 APPROVAL 3 处理中 PROCESSING 1
        if (taskCreate.checkTaskHasLaunch(curTask.getTaskGroupId())) {
            Long launchTaskkId = IdUtil.getSnowflakeNextId();
            taskCreate.createLaunchTask(eProcessInstances,
                                        toNode,
                                        null,
                                        TaskStatus.APPROVAL,
                                        launchTaskkId,
                                        null,
                                        approverTask.getID(),
                                        liableUser,
                                        taskGroupId,
                                        JSONObject.toJSONString(formValue),
                                        TaskActionType.LOG_TASK);
        }
        Integer requiredApprovalCount = eProcessInstances.getRequiredApprovalCount();
        nodeUtils.setEProcessInstancesApproverCount(eProcessInstances,
                                                    "+",
                                                    1);
        //  当前节点全部审批完成
        if (requiredApprovalCount <= eProcessInstances.getNodeApprovedCount()) {
            // 创建 审批人任务 APPROVER_TASK 2 下一个 审批中 UNDER_APPROVAL 2 处理中 PROCESSING 1
            String nextProcessDesignId = handlerNextNode(approverTask.getProcessDesignId(),
                                                         formValue,
                                                         eProcessInstances,
                                                         links,
                                                         approverTask,
                                                         taskGroupId,
                                                         TaskActionType.LOG_TASK,
                                                         approverTaskId);
            if (nextProcessDesignId != null) {
                //重置审批节点的审批数量
                resetEProcessInstancesApproverCount(eProcessInstances,
                                                    nextProcessDesignId,
                                                    0);
            }
            //清除操作组ID
            taskCreate.clearOperationGroupId(eProcessInstances);
        }
        // 处理中 PROCESSING 1
        eProcessInstances.setFlowStatus(FlowStatus.PROCESSING.getValue().longValue());
        updateEProcessInstances(eProcessInstances);
        return approverTask.getID();
    }
}